package com.changgou.system.service.impl;

import com.changgou.system.dao.AdminMapper;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * @param admin
     * @return void
     * @description: 添加管理员
     * @author 大佬味的小男孩
     * @date 2020/07/17 22:57
     */
    @Override
    @Transactional
    public void add(Admin admin) {
        //获取盐
        String gensalt = BCrypt.gensalt();
        //获取密文
        String hashPassword = BCrypt.hashpw(admin.getPassword(), gensalt);
        admin.setPassword(hashPassword);
        adminMapper.insert(admin);
    }

    /**
     * @param admin
     * @return boolean
     * @description: 管理员登录
     * @author 大佬味的小男孩
     * @date 2020/07/18 09:43
     */
    @Override
    public boolean login(Admin admin) {
       /* //用户输入的密码
        String loginpassword = admin.getPassword();*/
        //数据库中查询到的管理员数据
        Admin Resultadmin = adminMapper.findByUsername(admin.getLoginName());
        System.out.println("查询数据:" + Resultadmin);
        //用户不存在
        if (Resultadmin == null) {
            return false;
        }
        //验证密码 第一个参数为用户登录输入的明文密码 第二个参数为数据库的查询出来的密文
        boolean checkpw = BCrypt.checkpw(admin.getPassword(), Resultadmin.getPassword());
        return checkpw;
    }

    /**
     * @param id
     * @return void
     * @description: 根据id删除管理员
     * @author 大佬味的小男孩
     * @date 2020/07/18 00:05
     */
    @Override
    @Transactional
    public Integer delete(Integer id) {
        int i = adminMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * @param
     * @return java.util.List<com.changgou.system.pojo.Admin>
     * @description: 查询所有管理员
     * @author 大佬味的小男孩
     * @date 2020/07/18 00:12
     */
    @Override
    public List<Admin> findAll() {
        List<Admin> adminList = adminMapper.selectAll();
        return adminList;
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 用户名
            if (searchMap.get("loginName") != null && !"".equals(searchMap.get("loginName"))) {
                criteria.andLike("loginName", "%" + searchMap.get("loginName") + "%");
            }
            // 密码
            if (searchMap.get("password") != null && !"".equals(searchMap.get("password"))) {
                criteria.andLike("password", "%" + searchMap.get("password") + "%");
            }
            // 状态
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

            // id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
        }
        return example;
    }


}
